package entities 
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	import pure.engine.renderer.IShape;
	
	import examples.Box2DWorld;
	
	import pure.engine.entity.IEntity;
	import pure.engine.entity.IEntityFactory;
	import pure.engine.renderer.AnimationRenderer;
	import pure.engine.renderer.ShapeRenderer;
	
	/**
	 * ...
	 * @author Pure
	 */
public class PhysicsShape  implements IEntityFactory 
{
	
	// args:hw,hh
	public function produce(entity:IEntity, wx:Number, wy:Number, args:Array = null):IEntity
	{
		var width:Number = args[0]*2;// half width
		var height:Number = args[1]*2;// half height
		var steady:Boolean = args[2];
		
		entity.visible = true;
		var R:IShape = entity.createShapeRenderer(0,0,null,true);

			R.graphics.beginFill(Math.random()*0xFFFFFF,0.9);
			R.graphics.lineStyle(1, 0, 0.9);
			//R.graphics.drawCircle(width,width, width);
			R.graphics.drawRect(0, 0, width, height);
			R.end();
		
		var bodyDef:b2BodyDef = new b2BodyDef();
		if (!steady)
		{
			bodyDef.type = b2Body.b2_dynamicBody;
		}
		bodyDef.position.Set(wx / 30, wy / 30);
		var body:b2Body=Box2DWorld.world.CreateBody(bodyDef);

		var shape:b2PolygonShape = new b2PolygonShape();
		shape.SetAsBox(width/2 / 30, height/2 / 30);

		//var shape:b2CircleShape = new b2CircleShape();
		//shape.SetRadius(width / 30);
		
		body.SetUserData(entity);
		var fixtureDef:b2FixtureDef = new b2FixtureDef();
		fixtureDef.shape=shape;
		fixtureDef.density = 1;
		fixtureDef.friction = 0.3;
		fixtureDef.restitution = 0.4;
		body.CreateFixture(fixtureDef);

		return entity;
	}
}

}